package com.sigmatrix.h5.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sigmatrix.h5.dao.InspectionRecordsDao;
import com.sigmatrix.h5.dao.InspectionRecordsDetailDao;
import com.sigmatrix.h5.dao.ProductDao;
import com.sigmatrix.h5.entity.InspectionRecords;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.h5.utils.SimpleRowMapper;
import com.sigmatrix.h5.web.args.InsParam;
import com.sigmatrix.h5.web.mode.Inspection;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;
import com.sigmatrix.sc.jdbc.common.lang.Page;
/**
 * 
 *@ClassName: InspectionRecordsDaoImpl
 *@Description: 
 *@author MH
 *@date 2017年9月21日 下午3:15:54
 */
@Repository
public class InspectionRecordsDaoImpl extends DaoImpl<InspectionRecords,Long> implements InspectionRecordsDao{
	
	@Autowired
	InspectionRecordsDetailDao detailDao;
	
	@Autowired
	ProductDao productDao;
	
	@Override
	public Inspection detail(Long id) {
		StringBuffer sql = selectSQL();
		sql.append(" and a._id = ?");
		List<Inspection> list = query(sql.toString(), SimpleRowMapper.createDefault(Inspection.class), id);
		return CommonUtils.isEmpty(list)?null:list.get(0);
	}

	@Override
	public Page<Inspection> findByCondition(InsParam vo,int pageNo,int pageSize) {
		StringBuffer sql = selectSQL();
		List<Object> args = new ArrayList<Object>();
//		if(CommonUtils.isNotEmpty(vo.getCodeFirstDealerId())){
//			sql.append(" and a._code_first_dealer_id = ? ");
//			args.add(vo.getCodeFirstDealerId());
//		}
//		if(CommonUtils.isNotEmpty(vo.getAccount())){
//			sql.append(" and a._account = ? ");
//			args.add(vo.getAccount());
//		}
		if(CommonUtils.isNotEmpty(vo.getStatus())){
			sql.append(" and a._status = ? ");
			args.add(vo.getStatus());
		}
		return query(sql.toString(), null, 
				pageNo, pageSize, SimpleRowMapper.createDefault(Inspection.class), args.toArray());
	}

	private StringBuffer selectSQL(){
		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		sql.append("a._id,a._account,a._create_time,a._product_id,a._qr_code,a._status,");
		sql.append("b._area_inspection_status,b._batch_code,b._code_first_dealer_id,b._code_level,");
		sql.append("b._code_type,b._dealer_inspection_status,b._inspection_city_id,b._inspection_city_sub_ordinate,");
		sql.append("b._inspection_dealer_id,b._inspection_province_id,b._inspection_records_id,b._inspection_status,");
		sql.append("b._out_storage_time,b._product_time,b._remark,b._scan_city_id,b._scan_city_sub_ordinate,");
		sql.append("b._scan_ip,b._scan_province_id,b._type,b._visual_code,b._wrap_name,b._seq_num,");
		sql.append("c._name _product_name ");
		sql.append("from ");
		sql.append(productDao.tableName()).append(" c,");
		sql.append(tableName()).append(" a,");
		sql.append(detailDao.tableName()).append(" b ");
		sql.append("where a._id = b._inspection_records_id");
		sql.append(" and a._product_id = c._id ");
		sql.append(" and a._status <> ").append(Constants.STATUS_DELETE);
		return sql;
	}
	

}
